﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web.Models;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using DataLibrary;
using DataLibrary.Entity;

namespace Web.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class CheckController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        private readonly UpgradeDbContext _dbContext;

        public CheckController(IHostingEnvironment hostingEnvironment, UpgradeDbContext dbContext)
        {
            _hostingEnvironment = hostingEnvironment;
            _dbContext = dbContext;
        }

        [HttpGet]
        public FileConfigModel Get(string AppId)
        {
            List<FileInfoModel> list = new List<FileInfoModel>();

            var fileMagic = (from f in _dbContext.FileMagics
                             join p in _dbContext.Products
                             on f.ProductId equals p.Id
                             where p.Id == AppId
                             select f)
                            .AsNoTracking()
                            .ToList();

            foreach (FileMagic f in fileMagic)
            {
                list.Add(new FileInfoModel()
                {
                    Id = f.Id,
                    Name = f.Name,
                    Md5 = f.MD5,
                    Folder = f.Folder
                });
            }
            var _product = _dbContext.Products.FirstOrDefault(x => x.Id == AppId);

            FileConfigModel config = new FileConfigModel();
            if (_product != null)
            {
                config.FileInfo = list;
                config.UpExplain = _product.UpExplain;
            }

            return config;
        }
    }
}